from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from pandas import Series, DataFrame
import joblib
import pandas as pd
import numpy as np

"注意：TUKI_DATA和TOIMTUKI_DATA两列数据相同"

def linear1():
    # 正规方程的优化方法对TUKI_DATA进行预测
    # 1.获取数据
    # 获取数据（利用pd读取数据）
    Eigen_Value = pd.read_csv(r"C:\Users\Lenovo\Desktop\机器学习数据\JUTTA DATA\最终筛选Eigen_Value.csv")

    # 2.划分数据集
    # 划分特征矩阵和标签
    X1 = Eigen_Value.loc[: , Eigen_Value.columns != 'TOIMTUKI_DATA']
    X2 = Eigen_Value.loc[: , Eigen_Value.columns != 'LUKU_DATA']
    y1 = Eigen_Value.loc[: , Eigen_Value.columns == 'TOIMTUKI_DATA']
    y2 = Eigen_Value.loc[: , Eigen_Value.columns == 'LUKU_DATA']

    x_train, x_test, y_train, y_test = train_test_split(X1, y1, random_state=22)
    # x_train, x_test, y_train, y_test = train_test_split(X2, y2, random_state=22)

    # 3.预估器
    estimator = LinearRegression()
    estimator.fit(x_train, y_train)

    # 4.得出模型
    print("正规方程权重系数为：\n", estimator.coef_)
    print("正规方程偏置为： \n", estimator.intercept_)

    # 5.模型评估
    y_predict = estimator.predict(x_test)
    print("预测TUKI_DATA：\n", y_predict)
    error = mean_squared_error(y_test,y_predict)
    print("正规方程均方误差为：\n",error)

def linear2():
    # 梯度下降的优化方法对TUKI_DATA进行预测
    # 1.获取数据
    # 获取数据（利用pd读取数据）
    Eigen_Value = pd.read_csv(r"C:\Users\Lenovo\Desktop\机器学习数据\JUTTA DATA\最终筛选Eigen_Value.csv")

    # 2.划分数据集
    # 划分特征矩阵和标签
    X1 = Eigen_Value.loc[: , Eigen_Value.columns != 'TOIMTUKI_DATA']
    X2 = Eigen_Value.loc[: , Eigen_Value.columns != 'LUKU_DATA']
    y1 = Eigen_Value.loc[: , Eigen_Value.columns == 'TOIMTUKI_DATA']
    y2 = Eigen_Value.loc[: , Eigen_Value.columns == 'LUKU_DATA']

    x_train, x_test, y_train, y_test = train_test_split(X1, y1, random_state=22)
    # x_train, x_test, y_train, y_test = train_test_split(X2, y2, random_state=22)

    # 3.预估器
    estimator = SGDRegressor()
    estimator.fit(x_train, y_train)

    # 4.得出模型
    print("梯度下降权重系数为：\n", estimator.coef_)
    print("梯度下降偏置为： \n", estimator.intercept_)

    # 5.模型评估
    y_predict = estimator.predict(x_test)
    print("预测TUKI_DATA：\n", y_predict)
    error = mean_squared_error(y_test,y_predict)
    print("梯度下降均方误差为：\n",error)

def linear3():
    # 岭回归的优化方法对TUKI_DATA进行预测
    # 1.获取数据
    # 获取数据（利用pd读取数据）
    Eigen_Value = pd.read_csv(r"C:\Users\Lenovo\Desktop\机器学习数据\JUTTA DATA\最终筛选Eigen_Value.csv")

    # 2.划分数据集
    # 划分特征矩阵和标签
    X1 = Eigen_Value.loc[: , Eigen_Value.columns != 'TOIMTUKI_DATA']
    X2 = Eigen_Value.loc[: , Eigen_Value.columns != 'LUKU_DATA']
    y1 = Eigen_Value.loc[: , Eigen_Value.columns == 'TOIMTUKI_DATA']
    y2 = Eigen_Value.loc[: , Eigen_Value.columns == 'LUKU_DATA']

    x_train, x_test, y_train, y_test = train_test_split(X1, y1, random_state=22)
    # x_train, x_test, y_train, y_test = train_test_split(X2, y2, random_state=22)

    # 3.预估器
    estimator = Ridge()
    estimator.fit(x_train, y_train)

    # 保存模型
    joblib.dump(estimator, "my_ridge.pkl")
    # 加载模型
    estimator = joblib.load("my_ridge.pkl")

    # 4.得出模型
    print("岭回归权重系数为：\n", estimator.coef_)
    print("岭回归偏置为： \n", estimator.intercept_)

    # 5.模型评估
    y_predict = estimator.predict(x_test)
    print("预测TUKI_DATA：\n", y_predict)
    error = mean_squared_error(y_test,y_predict)
    print("岭回归均方误差为：\n",error)


if __name__ == "__main__":
    # 正规方程预测：
    linear1()
    # 梯度下降预测：
    linear2()
    # 岭回归预测：
    linear3()